package com.geen.tuoke.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.fragment.app.activityViewModels
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseFragment
import com.geen.tuoke.databinding.FragmentPhoneBinding
import com.geen.tuoke.ui.activity.ImportWxFileActivity
import com.geen.tuoke.ui.activity.TaskDetailActivity
import com.geen.tuoke.ui.activity.phone.PhoneNumberActivity
import com.geen.tuoke.ui.activity.phone.SearchTaskActivity
import com.geen.tuoke.ui.adapter.PhoneTaskAdapter
import com.geen.tuoke.ui.dialog.DialogDataImport
import com.geen.tuoke.ui.viewmodel.MainViewModel
import com.geen.tuoke.ui.viewmodel.PhoneFragmentViewModel
import com.geen.tuoke.utils.ext.dp2px
import com.geen.tuoke.utils.ext.initClose
import com.geen.tuoke.utils.ext.startActivity
import com.geen.tuoke.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.include_toolbar.*


/**
 * @Author LuoJi
 * @Date 2021/12/24-10:17
 * @Desc
 */
class PhoneFragment : BaseFragment<PhoneFragmentViewModel, FragmentPhoneBinding>() {

    private val mainViewModel by activityViewModels<MainViewModel>()
    private var dataImportDialog: DialogDataImport? = null
    private lateinit var mAdapter: PhoneTaskAdapter

    override fun getLayoutId(): Int {
        return R.layout.fragment_phone
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        with(mBinding) {
            lifecycleOwner = viewLifecycleOwner
            toolbar.initClose("拨打电话", R.mipmap.icon_search) {
                startActivity<SearchTaskActivity>()
            }
            viewCallPhone.setOnClickListener {
                startActivity<PhoneNumberActivity>()
            }

            viewImport.setOnClickListener {
                dataImportDialog?.showDialog()
            }
            mainViewModel.tabPhoneRestart.observe(viewLifecycleOwner, {
                if (it) {
                    getPhoneTaskData(true)
                }
            })
            initView()
        }

        mViewModel.datapage.observe(viewLifecycleOwner, {
            if (it != null) {
                if (mViewModel.pageNo == 1) {
                    mAdapter.setList(it.records)
                } else {
                    mAdapter.addData(it.records)
                }
                if (mViewModel.pageNo == it.totalPage) {
                    mAdapter.loadMoreModule.loadMoreEnd()
                    // 最下方有两个按钮 可能会遮挡
                    addFootView()
                } else {
                    mAdapter.loadMoreModule.loadMoreComplete()
                }
                mViewModel.pageNo++
            }
            mBinding.swipeRefresh.isRefreshing = false
        })
    }

    private fun initView() {

        mBinding.swipeRefresh.setOnRefreshListener {
            getPhoneTaskData(false)
        }

        mBinding.swipeRefresh.setColorSchemeColors(
            ContextCompat.getColor(
                requireContext(),
                R.color.colorPrimary
            )
        )

        dataImportDialog = DialogDataImport(requireContext(), clickType = {
            dataImportDialog?.dismiss()
            when (it) {
                DialogDataImport.TYPE_WECHATE -> {
                    startActivity<ImportWxFileActivity>()
                }
                DialogDataImport.TYPE_COMPUTER -> {

                }
                DialogDataImport.TYPE_CAMERA -> {

                    mainViewModel.takePicture.value = true
                }
            }
        })
        mAdapter = PhoneTaskAdapter()
        mBinding.mRecycleView.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val item = mAdapter.data[position]
            TaskDetailActivity.start(view.context, item)
        }
        // 处理分页
        mAdapter.loadMoreModule.loadMoreView = CustomLoadMoreView()
        mAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = true
        mAdapter.loadMoreModule.isEnableLoadMore = true
        mAdapter.loadMoreModule.isAutoLoadMore = true

        mAdapter.loadMoreModule.setOnLoadMoreListener {
            mViewModel.getPhoneTask(false)
        }

    }

    private fun addFootView() {
        val footView = layoutInflater.inflate(R.layout.empty_foot, null)
        val viewHeight = footView.findViewById<View>(R.id.viewHeight)
        val layoutParams = viewHeight.layoutParams
        layoutParams.height = dp2px(80f)
        mAdapter.addFooterView(footView)
    }

    private fun getPhoneTaskData(showLoading: Boolean = true) {
        mViewModel.pageNo = 1
        mViewModel.getPhoneTask(showLoading)
        mAdapter.removeAllFooterView()
    }

    override fun onLazyLoad() {
        super.onLazyLoad()
        getPhoneTaskData()
    }
}